package com.vtuzx.kingdee.xqc_core.utils;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxHttpUtil;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_core.constant.IEzrConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

public class EzrUtil {
    /** Logger */
    private static final Logger _logger = LoggerFactory.getLogger(EzrUtil.class);



    /**
     * 执行API
     *
     * @param param 入参
     * @return 执行结果
     * @throws Exception 异常
     */
    public static VtuzxMap callApi(String method,  Map<String,Object> param) throws Exception {
//        _logger.info(String.format("EZR参数:%s", param.toString()));
        String timestamp = VtuzxUtil.formatDate(new Date(),new SimpleDateFormat("yyyyMMddHHmmss"));
        String paramStr = VtuzxUtil.json2Str(param);
        _logger.info(String.format("EZR参数:%s", paramStr));
        //sha1加密然后大写获取sign
        Map<String,Object> signMap = new LinkedHashMap<>();
        signMap.put("AppId", IEzrConst.AppId);
        signMap.put("Timestamp", timestamp);
        signMap.put("Token", IEzrConst.Token);
        List<String> arr = new ArrayList<>(signMap.keySet());
        StringBuilder content = new StringBuilder();
        for (String key : arr) {
            if (content.length() > 0) {
                content.append("&");
            }
            content.append(key).append("=").append(VtuzxUtil.getString(signMap, key));
        }
        System.out.println(content);
        String sign = content.toString();
        String sign1 = VtuzxUtil.sha1(sign);
        sign = sign1.toUpperCase();

        VtuzxMap paramMap = new VtuzxMap();
        paramMap.put("AppId", IEzrConst.AppId);
        paramMap.put("Timestamp",timestamp);
        paramMap.put("Sign",sign);
        paramMap.put("AppSystem",IEzrConst.AppSystem);
        paramMap.put("Args","[" + paramStr + "]");


        StringBuilder paramBuilder = new StringBuilder();
        List<String> paramKeys = new ArrayList<>(paramMap.keySet());
        for (String key : paramKeys) {
            if (paramBuilder.length() > 0) {
                paramBuilder.append("&");
            }
            if ("Args".equals(key)) {
                paramBuilder.append(key).append("=").append(URLEncoder.encode(VtuzxUtil.getString(paramMap, key), "UTF-8"));
            } else {
                paramBuilder.append(key).append("=").append(VtuzxUtil.getString(paramMap, key));
            }
        }
        System.out.println(paramBuilder.toString());

        VtuzxMap res = null;
        try {
            String baseUrl = "https://open-tp.ezrpro.com/api/pbase/ProdUploadMethod";
            _logger.info("EZR callApi url:" + baseUrl);
            _logger.info("EZR callApi param:" + param);
            String resStr = VtuzxHttpUtil.post(baseUrl, paramBuilder.toString());
            res = VtuzxUtil.str2Json(resStr);
        } catch(Exception e) {
                throw e;
//            return callApi( method, param);
        }
        String code = VtuzxUtil.getString(res, "StatusCode");
        if ("200".equals(code)) {
            return res;
        }
        throw new VtuzxException(VtuzxUtil.getString(res, "msg"));
    }

}
